package com.study.talk.ui.adapter.moment

import android.view.LayoutInflater
import android.view.MenuItem
import android.view.ViewGroup
import android.widget.PopupMenu
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.RecyclerView
import com.study.talk.R
import com.study.talk.databinding.MomentsCommentsItemBinding
import com.study.talk.data.model.MomentComment
import com.study.common.ToastUtil
import com.study.talk.data.repository.MomentRepository
import com.study.talk.ui.adapter.base.BaseListAdapter
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

class MomentCommentsAdapter(
    private val mine: Boolean,
    private val userId: Int,
    private val momentRepository: MomentRepository
) : BaseListAdapter<MomentComment, MomentCommentsAdapter.ViewHolder>() {


    class ViewHolder(val binding: MomentsCommentsItemBinding) :
        RecyclerView.ViewHolder(binding.root)

    /** 创建ViewHolder  */
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        return ViewHolder(
            DataBindingUtil.inflate(
                LayoutInflater.from(parent.context), R.layout.moments_comments_item, parent, false
            )
        )
    }


    /** 绑定ViewHolder 数据  */
    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.binding.comment = getItem(holder.adapterPosition)
        // 设置评论列表项的长按事件
        holder.binding.root.setOnLongClickListener { v ->
            // 创建弹出菜单
            if (mine || getItem(holder.adapterPosition).userId == userId) {
                val popupMenu = PopupMenu(v.context, v)
                popupMenu.menuInflater.inflate(R.menu.delete, popupMenu.menu)
                popupMenu.show()
                // 设置菜单项的点击事件
                popupMenu.setOnMenuItemClickListener { item: MenuItem ->
                    if (item.itemId == R.id.menu_delete) {
                        // 删除评论
                        CoroutineScope(Dispatchers.IO).launch {
                            momentRepository.deleteComment(getItem(holder.adapterPosition),
                                success = {
                                    launch(Dispatchers.Main) {
                                        submitList(it)
                                    }
                                },
                                fail = { msg: String ->
                                    ToastUtil.showToast(
                                        v.context, msg
                                    )
                                })
                        }
                        /*NetApi.deleteMomentComment(getItem(holder.adapterPosition), success = {
                            submitList(it)
                        }, fail = { msg: String ->
                            ToastUtil.showToast(
                                v.context, msg
                            )
                        })*/
                    }
                    true
                }
            }
            true
        }
    }

}
